﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;
using Joyintech.Common;

namespace Joyintech.Admin.inpiao365.general.template.match
{
    public partial class index : System.Web.UI.Page
    {
        #region 页面初始化

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //加载栏目模板
                DataBindChannelTemplate();

                //加载内容模板
                DataBindContentTemplate();

                //加载栏目列表
                DataBindNode(1, "");
            }
        }

        #endregion

        #region 加载栏目列表

        /// <summary>
        /// 加载栏目列表
        /// </summary>
        /// <param name="ParentId"></param>
        private void DataBindNode(int ParentId, string strFront)
        {
            List<systemNode> list = new List<systemNode>();

            list = new systemNode_BLL().GetModelList("ParentID=" + ParentId + " order by NodeOrder");

            if (list != null && list.Count > 0)
            {
                strFront += "-";

                foreach (systemNode nodeModel in list)
                {
                    ListItem item = new ListItem();

                    item.Text = strFront + nodeModel.NodeName;

                    if (nodeModel.ChannelTemplateID != null && nodeModel.ChannelTemplateID.Value.ToString() != "")
                    {
                        ListItem channel = this.listChannelTemplate.Items.FindByValue(nodeModel.ChannelTemplateID.Value.ToString());

                        if (channel != null)
                        {
                            item.Text += "(" + channel.Text + ")";
                        }
                        else
                        {
                            item.Text += "(未指定)";
                        }
                    }
                    else
                    {
                        item.Text += "(未指定)";
                    }

                    if (nodeModel.ContentTemplateID != null && nodeModel.ContentTemplateID.Value.ToString() != "")
                    {
                        ListItem channel = this.listContentTemplate.Items.FindByValue(nodeModel.ContentTemplateID.Value.ToString());

                        if (channel != null)
                        {
                            item.Text += "(" + channel.Text + ")";
                        }
                        else
                        {
                            item.Text += "(未指定)";
                        }
                    }
                    else
                    {
                        item.Text += "(未指定)";
                    }

                    item.Value = nodeModel.NodeID.ToString();

                    this.listSystemNode.Items.Add(item);

                    DataBindNode(nodeModel.NodeID, strFront);
                }
            }
        }

        #endregion

        #region 加载栏目模板

        /// <summary>
        /// 加载栏目模板
        /// </summary>
        /// <param name="ParentId"></param>
        private void DataBindChannelTemplate()
        {
            List<systemTemplate> list = new List<systemTemplate>();

            list = new systemTemplate_BLL().GetModelList("TemplateType='ChannelTemplate' order by TemplateId");

            if (list != null && list.Count > 0)
            {
                foreach (systemTemplate model in list)
                {
                    ListItem item = new ListItem(model.TemplateName, model.TemplateId.ToString());

                    this.listChannelTemplate.Items.Add(item);
                }
            }
        }

        #endregion

        #region 加载内容模板

        /// <summary>
        /// 加载内容模板
        /// </summary>
        /// <param name="ParentId"></param>
        private void DataBindContentTemplate()
        {
            List<systemTemplate> list = new List<systemTemplate>();

            list = new systemTemplate_BLL().GetModelList("TemplateType='ContentTemplate' order by TemplateId");

            if (list != null && list.Count > 0)
            {
                foreach (systemTemplate model in list)
                {
                    ListItem item = new ListItem(model.TemplateName, model.TemplateId.ToString());

                    this.listContentTemplate.Items.Add(item);
                }
            }
        }

        #endregion

        protected void btnMatchChannel_Click(object sender, EventArgs e)
        {
            systemNode_BLL bll = new systemNode_BLL();

            List<systemNode> list = new List<systemNode>();

            foreach (ListItem itemSystemNode in this.listSystemNode.Items)
            {
                if (itemSystemNode.Selected)
                {
                    foreach (ListItem itemChannelTemplate in this.listChannelTemplate.Items)
                    {
                        if (itemChannelTemplate.Selected)
                        {
                            systemNode model = new systemNode();
                            model.NodeID = Convert.ToInt32(itemSystemNode.Value);

                            model = bll.GetModel(model.NodeID);

                            model.ChannelTemplateID = Convert.ToInt32(itemChannelTemplate.Value);

                            //将选中的栏目及栏目模板匹配结果存入list
                            list.Add(model);
                        }
                    }
                }
            }

            if (list.Count > 0)
            {
                int Num = bll.UpdateList(list);
                MessageBox.Show(this, "匹配成功！");
            }
        }

        protected void btnMatchChannelClear_Click(object sender, EventArgs e)
        {

        }

        protected void btnMatchContent_Click(object sender, EventArgs e)
        {

            systemNode_BLL bll = new systemNode_BLL();

            List<systemNode> list = new List<systemNode>();

            foreach (ListItem itemSystemNode in this.listSystemNode.Items)
            {
                if (itemSystemNode.Selected)
                {
                    foreach (ListItem itemContentTemplate in this.listContentTemplate.Items)
                    {
                        if (itemContentTemplate.Selected)
                        {
                            systemNode model = new systemNode();
                            model.NodeID = Convert.ToInt32(itemSystemNode.Value);

                            model = bll.GetModel(model.NodeID);

                            model.ContentTemplateID = Convert.ToInt32(itemContentTemplate.Value);

                            //将选中的栏目及栏目模板匹配结果存入list
                            list.Add(model);
                        }
                    }
                }
            }

            if (list.Count > 0)
            {
                int Num = bll.UpdateList(list);
                MessageBox.Show(this, "匹配成功！");
            }
        }

        protected void btnMatchContentClear_Click(object sender, EventArgs e)
        {

        }
    }
}